home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicButtonUI.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  222 lines

  1. /*
  2.  * @(#)OrganicButtonUI.java    1.5 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * OrganicButtonUI implementation
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.5 02/02/98
  41.  * @author Michael C. Albers
  42.  * @author Tom Santos
  43.  */
  44. public class OrganicButtonUI extends BasicButtonUI {
  45.  
  46.   protected Color getSelectColor()       { return UIManager.getColor("Button.pressed"); }
  47.   protected Color getDisabledColor()     { return UIManager.getColor("Button.disabled"); }
  48.   protected Color getDisabledTextColor() { return UIManager.getColor("Button.disabledText"); }
  49.   protected Color getFocusColor() { return UIManager.getColor("Button.focus"); }
  50.  
  51.   // We pass null here because nobody actually uses the component any more.
  52.   protected OrganicButtonListener organicButtonListener = new OrganicButtonListener( null );
  53.  
  54.   private final static OrganicButtonUI organicButtonUI = new OrganicButtonUI(); 
  55.  
  56.   // visual constants
  57.   private static final int defaultTextIconGap = 4;
  58.  
  59.  
  60.   public static ComponentUI createUI(JComponent c) {
  61.     return organicButtonUI;
  62.   }
  63.  
  64.  
  65.   protected BasicButtonListener createListener(JComponent c) {
  66.     return organicButtonListener;
  67.   }
  68.   
  69.   
  70.   /**
  71.    * ComponentUI implementation for OrganicButton
  72.    *
  73.    */
  74.   public void paint(Graphics g, JComponent c) {
  75.     AbstractButton b = (AbstractButton) c;
  76.     ButtonModel model = b.getModel();
  77.     
  78.     Dimension size = b.getSize();
  79.     FontMetrics fm = g.getFontMetrics();
  80.     
  81.     int shift_offset = 0;
  82.     
  83.     Insets i = getInsets(c);
  84.     
  85.     Rectangle viewRect = new Rectangle(size);
  86.     
  87.     viewRect.x += i.left;
  88.     viewRect.y += i.top;
  89.     viewRect.width -= (i.right + viewRect.x);
  90.     viewRect.height -= (i.bottom + viewRect.y);
  91.     
  92.     Rectangle iconRect = new Rectangle();
  93.     Rectangle textRect = new Rectangle();
  94.     
  95.     Font f = c.getFont();
  96.     g.setFont(f);
  97.     
  98.     // layout the text and icon
  99.     String text = SwingUtilities.layoutCompoundLabel(
  100.     fm, b.getText(), b.getIcon(),
  101.     b.getVerticalAlignment(), b.getHorizontalAlignment(),
  102.     b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  103.     viewRect, iconRect, textRect,
  104.     b.getText() == null ? 0 : defaultTextIconGap);
  105.     
  106.     if (model.isArmed() && model.isPressed()) {
  107.       // Note that in the Organic, things only offset to the left, that is, in X
  108.       //  They do NOT offset up or down (in Y)
  109.       shift_offset = 1;
  110.  
  111.       if (c.isOpaque()) {
  112.     g.setColor( getSelectColor() );
  113.     g.fillRect(1, 1, size.width-2, size.height-2);
  114.       }
  115.    }
  116.     
  117.     // Paint the Icon
  118.     boolean isIcon = b.getIcon() != null;
  119.     if(isIcon) { 
  120.       Icon icon = null;
  121.       if(!model.isEnabled()) {
  122.     icon = (Icon) b.getDisabledIcon();
  123.       } else if(model.isPressed() && model.isArmed()) {
  124.     icon = (Icon) b.getPressedIcon();
  125.     if(icon == null) {
  126.       // Use default icon
  127.       icon = (Icon) b.getIcon();
  128.     }
  129.       } else if(model.isRollover()) {
  130.     icon = (Icon) b.getRolloverIcon();
  131.       } 
  132.       
  133.       if (icon == null) {
  134.     icon = (Icon) b.getIcon();
  135.       }
  136.       
  137.     icon.paintIcon(c, g, iconRect.x - shift_offset, iconRect.y);
  138.     }
  139.     
  140.     // Draw the Text
  141.     if(text != null && !text.equals("")) {
  142.       if(!model.isEnabled()) {
  143.     // *** paint the text disabled
  144.     g.setColor(getDisabledTextColor());
  145.       } else {
  146.     // *** paint the text normally
  147.     g.setColor(c.getForeground());
  148.       }
  149.       g.drawString(text,
  150.            textRect.x - shift_offset,
  151.            textRect.y + fm.getAscent());
  152.     }
  153.     
  154.     // draw the dashed focus line.
  155.     if (b.isFocusPainted() && b.hasFocus()) {
  156.       // Draw each dash of the line pixel by pixel.
  157.       // The performance of this is surely poor -- Be sure
  158.       // to rewrite when 2d graphics package is ready.
  159.       Rectangle focusRect = new Rectangle();
  160.  
  161.       // If there is text
  162.       if ( text != null & !text.equals( "" ) ) {
  163.         if ( !isIcon ) {
  164.           focusRect.setBounds( textRect );
  165.       }
  166.       else {
  167.           focusRect.setBounds( iconRect.union( textRect ) );
  168.       }
  169.       }
  170.       // If there is an icon and no text
  171.       else if ( isIcon ) {
  172.       focusRect.setBounds( iconRect );
  173.       }
  174.  
  175.       paintFocus( g, (focusRect.x-1) - shift_offset, (focusRect.y-1),
  176.           focusRect.width+1, focusRect.height+1 );
  177.     }
  178.   }
  179.  
  180.   public void paintFocus( Graphics g, int x, int y, int w, int h ) {
  181.       g.setColor(OrganicLookAndFeel.getLightAccent1());
  182.       g.drawRect( x, y, w, h );
  183.       g.setColor(getFocusColor());
  184.       OrganicUtilities.drawDashedRect(g, x, y, w+1, h+1 );            
  185.   }
  186.   
  187.   
  188.   // I shouldn't need to redefine this.
  189.   // REMIND(mca)
  190.   public Insets getInsets(JComponent c) { 
  191.     Border border = c.getBorder();
  192.     Insets i = border != null? border.getBorderInsets(c) : new Insets(0,0,0,0);
  193.     return i;
  194.   }
  195.   
  196.  
  197.  
  198.  
  199.   /**
  200.    * Button margin.
  201.    * <p>
  202.    * Warning: serialized objects of this class will not be compatible with
  203.    * future swing releases.  The current serialization support is appropriate
  204.    * for short term storage or RMI between Swing1.0 applications.  It will
  205.    * not be possible to load serialized Swing1.0 objects with future releases
  206.    * of Swing.  The JDK1.2 release of Swing will be the compatibility
  207.    * baseline for the serialized form of Swing objects.
  208.    */
  209.   public static class OrganicButtonMargin extends AbstractBorder {
  210.     
  211.     public Insets getBorderInsets(Component c)       {
  212.       if (c instanceof AbstractButton) {
  213.     AbstractButton b = (AbstractButton)c;
  214.     return b.getMargin();
  215.       }
  216.       return new Insets(0, 0, 0, 0);
  217.     }
  218.   }
  219.   
  220. }
  221.  
  222.